<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        body {
            height: 1000px;
        }
        #box1{
            width: 200px;
            height: 200px;
            margin: 100px;
            /* margin-top: 500px; */
            background-color: pink;
        }
    </style>
</head>
<body>
    <div id="box1"></div>
    <script>
        // 获取元素
        var box1 = document.getElementById("box1");

        // 给同一个元素绑定不同事件类型
        // box1.onmouseover =function(){
        //     box1.style.backgroundColor = "skyblue";  
        // } 
        // box1.onmouseout =function(){
        //     box1.style.backgroundColor = "yellow";  
        // } 



        // 给同一个元素绑定不同事件类型,利用函数制作
        box1.onmouseover =fun;
        box1.onmouseout =fun;
        // 定义一个判断时间类型的函数
        function fun(e){
            e = e ||window.event;
            switch(e.type){
                case "mouseover":
                    box1.style.backgroundColor = "red";
                    break;
                case "mouseout":
                    box1.style.backgroundColor = "yellowgreen";
                    break;
            }
        }



    </script>
</body>
</html>